#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/6/6 11:25
# @Author  : ystraw
# @Site    : 
# @File    : xinlang.py
# @Software: PyCharm Community Edition
# @function:

import requests
import json
import time
import datetime

def getdata():
    # 获得查询的终点时间戳：
    d = datetime.datetime.strptime("2020-06-01 0:0:0", "%Y-%m-%d %H:%M:%S")
    t = d.timetuple()
    timeStamp = int(time.mktime(t))

    page = 0
    flag = 1
    alldata = [] # 存储数据
    # page 最大值在37左右：
    while flag == 1 and page < 4:
        try:
            page += 1
            print('page = ', page)
            # url = 'https://news.sina.com.cn/world/'
            url = 'https://interface.sina.cn/news/get_news_by_channel_new_v2018.d.html?cat_1=51923&show_num=27&level=1,2&page=%d&callback=newsloadercallback&_=1591414346860' % page
            kv = {'user-agent': 'Mozilla/5.0'}
            newsList = requests.get(url, headers=kv)
            # newsList.encoding = newsList.apparent_encoding

            response = newsList.text.encode('utf-8').decode('unicode_escape')
            # print(response)
            # 提取其中的json数据
            data = response[response.index('{'):-2]
            # print(data)
            js = json.loads(data, strict=False)
            newsList = js['result']['data']
            # print(newsList)
            for news in newsList:
                t_timeStamp = news['createtime']
                if int(t_timeStamp) < timeStamp:
                    print('获取结束')
                    flag = 0
                    break
                alldata.append(news['title'])
            print('page = ', page, '已成功获取！')
        except Exception as ex:
            print('异常：', ex)
        time.sleep(2)
    # 数据写入txt：
    content = ','.join(alldata)
    print(content)
    with open('./xinlang1.txt', 'w+', encoding='utf-8') as f:
        f.write(content)

if __name__ == '__main__':
    getdata()